import file from '@system.file';
import request from '@ohos.request';
import fetch from '@system.fetch';
import router from '@system.router';
import prompt from '@system.prompt';
import geolocation from '@system.geolocation';
import Service from '../../generated/TestService.js';
import dataStorage from '@ohos.data.storage'
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import featureAbility from '@ohos.ability.featureAbility';
import http from '@ohos.net.http';
import fileio from '@ohos.fileio';

export default {
    data: {
        //个人信息
        userid:null,
        username:null,
        password:null,
        email:null,
        headshot:null,
        sex:null,
        phonenumber:null,
        //路径信息
        context:null,
        path:null,
        cachepath:null,
        //图片信息
        imageName:null,
        imagePath:null,
        isChoosed:false,
        //场地名称
        parkName:null,
        //定位经纬度
        longitude:null,
        latitude:null,
    },
    async onInit(){
        this.context = featureAbility.getContext()
        this.path = await this.context.getFilesDir()
        this.cachepath = await this.context.getCacheDir()
    },
    async onShow(){
        let storage = dataStorage.getStorageSync(this.path + '/mystore')
        this.userid = storage.getSync('userid','-1');
        this.username = storage.getSync('username', '未登录')
        this.password = storage.getSync('password', '-1')
        this.email = storage.getSync('email', '-1')
        this.headshot = storage.getSync('headshot', '-1')
        this.sex = storage.getSync('sex', '-1')
        this.phonenumber = storage.getSync('phonenumber', '-1')

        this.getLocation()
    },
    TurnBack(){
        router.back();
    },
    ChooseCover(){
        let storage = dataStorage.getStorageSync(this.path + '/mystore')
        this.userid = storage.getSync('userid', '-1')
        if(this.userid === '-1'){
            prompt.showToast({
                message: '请先登录'
            })
            return;
        }

        var that = this
        let option = {type : "image", count : 1  };
        mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
            console.log("Media resources selected."+value);             // Obtain the media selection value.
            // 使用calculate同步方法，返回OperateData对象。
            this.utilInterface = createLocalParticleAbility('edu.zut.hys.jsskateboard.util.UtilServiceAbility');
            this.utilInterface.CopyToCache(value[0],result => {
                console.info(this.lable+result)
                that.imageName = result;
                that.imagePath = "internal://cache/"+result;
                prompt.showToast({
                    message: '选择成功'
                })
                that.UploadImage();
            })
        }).catch((err) => {
            console.log("An error occurred when selecting media resources.");
            prompt.showToast({
                message: '选择失败'
            })
        });
    },

    UploadImage(){
        if(this.imageName!==null)
        request.upload({ url: 'http://101.35.82.76:9001/upload',
            files:[{
                       filename: this.imageName,
                       name:'file',
                       uri:'internal://cache/'+this.imageName,
                       type:'png',
                   }],
            data:[{name:"userid",value:this.userid}]
        },
            (err, data) => {
                if (err) {
                    prompt.showToast({
                        message: '上传失败'
                    })
                    console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
                    return;
                }
                prompt.showToast({
                    message: '上传成功'
                })
                this.isChoosed = true
//                router.back();
            });
    },
    UploadPark(){
        var that = this
        let httpRequest= http.createHttp();
        var url;
        if(this.isChoosed === true){
            url = "http://101.35.82.76:9002/park/addPark?userid="+this.userid+
            '&parkName='+this.parkName+'&longitude='+this.longitude+'&latitude='+this.latitude+'&hasPic=true'
        }else{
            url = "http://101.35.82.76:9002/park/addPark?userid="+this.userid+
            '&parkName='+this.parkName+'&longitude='+this.longitude+'&latitude='+this.latitude+'&hasPic=false'
        }
        httpRequest.request( url ,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);
                    prompt.showToast({
                        message: '发布成功'
                    })
                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
    setParkName(param){
        this.parkName = param.text;
    },
    getLocation() {
        var that = this;
        geolocation.getLocation({
            success: function(data) {
                console.log('success get location data:' + data);
                that.latitude = data.latitude;
                that.longitude = data.longitude;
            },
            fail: function(data, code) {
                console.log('fail to get location. code:' + code + ', data:' + data);
                prompt.showToast({
                    message:'获取定位失败'
                })
            },
        });
    },
}
